﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StoreTransport.Business.Constants;
using StoreTransport.DAO;
using StoreTransport.Data;
using StoreTransport.Resources;
using StoreTransport.Business.Helper;

namespace StoreTransport.Presentation.Forms {
    public partial class AreaListForm : Form {
        int id = 0;
        Dictionary<string, string> regionDic = new Dictionary<string,string>();
        public AreaListForm() {
            InitializeComponent();
        }

        private void AreaListForm_Load(object sender, EventArgs e) {
            _tbxAreaName.Select();
            _drpRegionList.DisplayMember = ColumnName.RegionName;
            _drpRegionList.ValueMember = ColumnName.ID;
            StoreTransportDataContext context = CommonHelper.GetContext();
            _drpRegionList.DataSource = context.Regions.OrderBy(c => c.RegionName);
            regionDic = context.Regions.ToDictionary(c => c.ID.ToString(), c => c.RegionName);
            LoadArea();
        }

        private void LoadArea() {
            _grdAreas.AutoGenerateColumns = false;
            var areaList = CommonHelper.GetContext().Areas.
                Cast<StoreTransport.DAO.Area>().OrderBy(c => c.AreaName);
            List<PresentAreaDTO> prList = new List<PresentAreaDTO>();
            foreach (var r in areaList) {
                string region = string.Empty;
                if (r.Region != null && regionDic.ContainsKey(r.Region.ToString())) {
                    region = regionDic[r.Region.ToString()];
                }
                prList.Add(new PresentAreaDTO(r.ID, r.AreaName, region));
            }
            _grdAreas.DataSource = prList.OrderBy(c => c.AreaName).ToList();
        }

        private void _btxCreate_Click(object sender, EventArgs e) {
            ResetData();
        }

        private void ResetData() {
            _tbxAreaName.Select();
            _tbxAreaName.Text = string.Empty;
            _lblID.Text = StoreTransportResource.Form_NewID;
            id = 0;
            CommonHelper.ResetCombobox(_drpRegionList);
        }

        private void _btxSave_Click(object sender, EventArgs e) {
            StoreTransportDataContext context = CommonHelper.GetContext();
            string errorMessage = CommonHelper.CheckRequiredField(_tbxAreaName.Text, _lblAreaName.Text);
            if (!string.IsNullOrEmpty(errorMessage)) {
                MessageBox.Show(errorMessage);
                return;
            }
            Area area = null;
            if (id == 0) {
                if (context.Areas.FirstOrDefault(c => c.AreaName.Equals(_tbxAreaName.Text)) != null) {
                    MessageBox.Show(string.Format(StoreTransportResource.Error_Dupplicate_Area, _tbxAreaName.Text));
                    return;
                }
                area = new Area();
                context.Areas.InsertOnSubmit(area);
            } else {
                var item = context.Areas.FirstOrDefault(c => c.AreaName.Equals(_tbxAreaName.Text));
                if (item != null && item.ID != id) {
                    MessageBox.Show(string.Format(StoreTransportResource.Error_Dupplicate_Area, _tbxAreaName.Text));
                    return;
                }
                area = context.Areas.Where(c => c.ID.Equals(id)).First();
            }
            area.Region = (int)_drpRegionList.SelectedValue;
            area.AreaName = _tbxAreaName.Text;
            context.SubmitChanges();
            LoadArea();
        }

        private void _btnDelete_Click(object sender, EventArgs e) {
            if (_grdAreas.SelectedRows.Count > 0) {
                DialogResult result = MessageBox.Show(StoreTransportResource.Message_ConfirmDelete,
                    StoreTransportResource.MessageBox_ConfirmDelete, MessageBoxButtons.YesNo);
                if (result == System.Windows.Forms.DialogResult.Yes) {
                    try {
                        StoreTransportDataContext context = CommonHelper.GetContext();
                        foreach (DataGridViewRow row in _grdAreas.SelectedRows) {
                            id = int.Parse(row.Cells[0].Value.ToString());
                            Area selectedItem = context.Areas.Where(c => c.ID.Equals(id)).First();
                            context.Areas.DeleteOnSubmit(selectedItem);
                        }
                        context.SubmitChanges();
                        id = 0;
                        LoadArea();
                    } catch (Exception ex) {
                        MessageBox.Show(StoreTransportResource.ErrorMessage_CanNotDeleteItem);
                    }
                }
            } else {
                MessageBox.Show(StoreTransportResource.NoItemSelected);
            }
        }

        private void _btxEdit_Click(object sender, EventArgs e) {
            if (_grdAreas.SelectedRows.Count > 0) {
                id = int.Parse(_grdAreas.SelectedRows[0].Cells[0].Value.ToString());
                EditItem();
            }
        }

        private void EditItem() {
            id = int.Parse(_grdAreas.SelectedRows[0].Cells[0].Value.ToString());
            Area selectedItem = CommonHelper.GetContext().Areas.Where(c => c.ID.Equals(id)).First();
            _tbxAreaName.Text = selectedItem.AreaName;
            CommonHelper.SetComboboxValue(_drpRegionList, selectedItem.Region);
            _lblID.Text = id.ToString();
        }

        private void _grdAreas_CellDoubleClick(object sender, DataGridViewCellEventArgs e) {
            id = int.Parse(_grdAreas.Rows[e.RowIndex].Cells[0].Value.ToString());
            EditItem();
        }
    }
}
